package com.crm.service.statisticAnalysis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.model.common.dtos.PageResult;
import com.crm.model.common.dtos.ResponseResult;
import com.crm.model.common.enums.AppHttpCodeEnum;
import com.crm.model.statisticAnalysis.pojos.SellStatistics;
import com.crm.model.statisticAnalysis.vos.SellLineChartVO;
import com.crm.service.statisticAnalysis.mapper.SellMapper;
import com.crm.service.statisticAnalysis.service.SellService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class SellServiceImpl implements SellService {
    @Autowired
    private SellMapper sellMapper;

    /**
     * 销售分页条件查询
     *
     * @param pageNum
     * @param pageSize
     * @param department
     * @return
     */
    @Override
    public ResponseResult sellPage(Integer pageNum, Integer pageSize, String department) {
        //校验参数
        if (pageNum == null || pageSize == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        IPage<SellStatistics> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SellStatistics> lqw = new LambdaQueryWrapper<>();

        if (department != null) {
            lqw.eq(SellStatistics::getDepartment, department);
        }
        page = sellMapper.selectPage(page, lqw);

        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getRecords());

        return ResponseResult.okResult(pageResult);
    }

    /**
     * 销售折线图
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public ResponseResult sellLineChart(LocalDate begin, LocalDate end) {
        //时间集合
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);

        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        //返回VO
        List<SellLineChartVO> lineChartVOS = new ArrayList<>();

        //遍历每一天
        for (LocalDate date : dateList) {
            //一天最小时间 和 最大时间
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            //查询当天成交金额
            BigDecimal amount = sellMapper.countAmount(beginTime, endTime);

            SellLineChartVO lineChartVO = SellLineChartVO.builder()
                    .amount(amount)
                    .date(date)
                    .build();
            lineChartVOS.add(lineChartVO);
        }
        //返回数据
        return ResponseResult.okResult(lineChartVOS);
    }
}
